/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __mysql_set_analysis_data.c                        */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/12 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <mysql/mysql_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/mysql/config.h"

extern int __mysql_set_analysis_data(L7session_mysql_private *p_st_private
                                    , Conntrack *p_st_conn
                                    , Frhash_header *p_st_hash_report
                                    , Apm_packet *p_st_packet
                                    , char *p_data
                                    , unsigned int ui_len
                                    , struct timeval *p_st_timestamp);

extern void mysql_destroy_session(L7session_mysql **pp_session);
extern void __mysql_destroy_curr_info(L7session_mysql_private *p_st_private);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql *p_st_sess = NULL;
    Apm_packet st_packet;
    struct timeval st_time;
    char *p_data = (char *)frmalloc(128);
    Conntrack st_conn;
    
    p_st_sess = (L7session_mysql *)frmalloc(sizeof(L7session_mysql));
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(p_st_sess, 0x00, sizeof(L7session_mysql));
    memset(&st_packet, 0x00, sizeof(Apm_packet));
    memset(&st_time, 0x00, sizeof(struct timeval));
    frhash2_create(&st_hash_report, 1024);
    
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    memcpy(p_data, (char *)"\x26\x00\x00\x01\x85\xa6\x03\x00\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x72\x6f\x6f\x74\x00\x00", 42);
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = p_st_sess;

    i_ret = __mysql_set_analysis_data(&st_mysql, &st_conn, &st_hash_report, &st_packet, p_data, 42, &st_time);
    
    frfree(p_data);
    mysql_destroy_session(&st_mysql.p_st_sess);
    __mysql_destroy_curr_info(&st_mysql);
    
    frhash2_free(&st_hash_report, frdb_statement_node_free);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_SUCC);
    frhash2_free(&st_hash_report, NULL);
    return;
}

static void case2(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql *p_st_sess = NULL;
    Apm_packet st_packet;
    struct timeval st_time;
    char *p_data = (char *)frmalloc(128);
    Conntrack st_conn;
    
    p_st_sess = (L7session_mysql *)frmalloc(sizeof(L7session_mysql));
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(p_st_sess, 0x00, sizeof(L7session_mysql));
    memset(&st_packet, 0x00, sizeof(Apm_packet));
    memset(&st_time, 0x00, sizeof(struct timeval));
    frhash2_create(&st_hash_report, 1024);
    
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    
    st_fragment.i_remain_head_len = 1;
    st_fragment.arr_remain_packet_head[0] = 0x26;
    memcpy(p_data, (char *)"\x00\x00\x01\x85\xa6\x03\x00\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x72\x6f\x6f\x74\x00\x00", 41);
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = p_st_sess;

    i_ret = __mysql_set_analysis_data(&st_mysql, &st_conn, &st_hash_report, &st_packet, p_data, 41, &st_time);
    
    frfree(p_data);
    mysql_destroy_session(&st_mysql.p_st_sess);
    __mysql_destroy_curr_info(&st_mysql);
    
    frhash2_free(&st_hash_report, frdb_statement_node_free);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_SUCC);
    frhash2_free(&st_hash_report, NULL);
    return;
}

static void case3(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql *p_st_sess = NULL;
    Apm_packet st_packet;
    struct timeval st_time;
    char *p_data = (char *)frmalloc(128);
    char *p_new = (char *)frmalloc(128);
    Conntrack st_conn;
    
    p_st_sess = (L7session_mysql *)frmalloc(sizeof(L7session_mysql));
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(p_st_sess, 0x00, sizeof(L7session_mysql));
    memset(&st_packet, 0x00, sizeof(Apm_packet));
    memset(&st_time, 0x00, sizeof(struct timeval));
    memset(p_new, 0x00, 128);
    frhash2_create(&st_hash_report, 1024);
    
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    
    st_fragment.u64_remain_len = 1;
    st_fragment.u64_total_len = 38;
    st_fragment.u64_parsed_index = 1;
    memcpy(p_data, (char *)"\x85\xa6\x03\x00\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x72\x6f\x6f\x74\x00", 37);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 37);
    
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = p_st_sess;
    
    memcpy(p_new, (char *)"\x00\x21\x00\x00\x00\x03\x73\x65\x6c\x65\x63\x74\x20\x40\x40\x76\x65\x72\x73\x69\x6f\x6e\x5f\x63\x6f\x6d\x6d\x65\x6e\x74\x20\x6c\x69\x6d\x69\x74\x20\x31", 38);

    i_ret = __mysql_set_analysis_data(&st_mysql, &st_conn, &st_hash_report, &st_packet, p_new, 38, &st_time);
    
    frfree(p_data);
    frfree(p_new);
    mysql_destroy_session(&st_mysql.p_st_sess);
    __mysql_destroy_curr_info(&st_mysql);
    
    frhash2_free(&st_hash_report, frdb_statement_node_free);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    frhash2_free(&st_hash_report, NULL);
    return;
}

static void case4(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql *p_st_sess = NULL;
    Apm_packet st_packet;
    struct timeval st_time;
    char *p_data = (char *)frmalloc(128);
    char *p_new = (char *)frmalloc(128);
    Conntrack st_conn;
    
    p_st_sess = (L7session_mysql *)frmalloc(sizeof(L7session_mysql));
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(p_st_sess, 0x00, sizeof(L7session_mysql));
    memset(&st_packet, 0x00, sizeof(Apm_packet));
    memset(&st_time, 0x00, sizeof(struct timeval));
    memset(p_new, 0x00, 128);
    frhash2_create(&st_hash_report, 1024);
    
    st_packet.i_direction = FR_DIRECT_CLIENT2SRV;
    
    st_fragment.u64_remain_len = 1;
    st_fragment.u64_total_len = 38;
    st_fragment.u64_parsed_index = 1;
    memcpy(p_data, (char *)"\x85\xa6\x03\x00\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x72\x6f\x6f\x74\x00", 37);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 37);
    
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = p_st_sess;
    
    memcpy(p_new, (char *)"\x00", 1);

    i_ret = __mysql_set_analysis_data(&st_mysql, &st_conn, &st_hash_report, &st_packet, p_new, 1, &st_time);
    
    frfree(p_data);
    frfree(p_new);
    mysql_destroy_session(&st_mysql.p_st_sess);
    __mysql_destroy_curr_info(&st_mysql);
    
    frhash2_free(&st_hash_report, frdb_statement_node_free);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_SUCC);
    frhash2_free(&st_hash_report, NULL);
    return;
}

static void case5(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql *p_st_sess = NULL;
    Apm_packet st_packet;
    struct timeval st_time;
    char *p_data = (char *)frmalloc(128);
    char *p_new = (char *)frmalloc(128);
    Conntrack st_conn;
    
    p_st_sess = (L7session_mysql *)frmalloc(sizeof(L7session_mysql));
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(p_st_sess, 0x00, sizeof(L7session_mysql));
    memset(&st_packet, 0x00, sizeof(Apm_packet));
    memset(&st_time, 0x00, sizeof(struct timeval));
    memset(p_new, 0x00, 128);
    frhash2_create(&st_hash_report, 1024);
    
    st_packet.i_direction = FR_DIRECT_SRV2CLIENT;
    
    st_mysql.p_st_req = (L7_req_mysql *)frmalloc(sizeof(L7_req_mysql));
    memset(st_mysql.p_st_req, 0x00, sizeof(L7_req_mysql));
    st_fragment.i_remain_head_len = 1;
    st_fragment.arr_remain_packet_head[0] = 0x01;
    memcpy(p_data, (char *)"\x00\x00\x01\x01\x3a\x00\x00\x02\x03\x64\x65\x66\x00\x0b\x54\x41\x42\x4c\x45\x5f\x4e\x41\x4d\x45\x53\x00\x0f\x54\x61\x62\x6c\x65\x73\x5f\x69\x6e\x5f\x6d\x79\x73\x71\x6c\x0a\x54\x41\x42\x4c\x45\x5f\x4e\x41\x4d\x45\x0c\x08\x00\x40\x00\x00\x00\xfd\x01\x00\x00\x00\x00", 66);
    
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = p_st_sess;
    
    i_ret = __mysql_set_analysis_data(&st_mysql, &st_conn, &st_hash_report, &st_packet, p_data, 66, &st_time);
    
    frfree(p_data);
    frfree(p_new);
    mysql_destroy_session(&st_mysql.p_st_sess);
    __mysql_destroy_curr_info(&st_mysql);
    
    frhash2_free(&st_hash_report, frdb_statement_node_free);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_SUCC);
    frhash2_free(&st_hash_report, NULL);
    return;
}

static void case6(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql *p_st_sess = NULL;
    Apm_packet st_packet;
    struct timeval st_time;
    char *p_data = (char *)frmalloc(128);
    char *p_new = (char *)frmalloc(128);
    Conntrack st_conn;
    
    p_st_sess = (L7session_mysql *)frmalloc(sizeof(L7session_mysql));
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(p_st_sess, 0x00, sizeof(L7session_mysql));
    memset(&st_packet, 0x00, sizeof(Apm_packet));
    memset(&st_time, 0x00, sizeof(struct timeval));
    memset(p_new, 0x00, 128);
    frhash2_create(&st_hash_report, 1024);
    
    st_packet.i_direction = FR_DIRECT_SRV2CLIENT;
    
    st_mysql.p_st_req = (L7_req_mysql *)frmalloc(sizeof(L7_req_mysql));
    memset(st_mysql.p_st_req, 0x00, sizeof(L7_req_mysql));
    st_fragment.u64_remain_len = 1;
    st_fragment.u64_parsed_index = 1;
    st_fragment.u64_total_len = 1;
    
    memcpy(p_data, (char *)"\x01", 1);
    memcpy(p_new, (char *)"\x01\x3a\x00\x00\x02\x03\x64\x65\x66\x00\x0b\x54\x41\x42\x4c\x45\x5f\x4e\x41\x4d\x45\x53\x00\x0f\x54\x61\x62\x6c\x65\x73\x5f\x69\x6e\x5f\x6d\x79\x73\x71\x6c\x0a\x54\x41\x42\x4c\x45\x5f\x4e\x41\x4d\x45\x0c\x08\x00\x40\x00\x00\x00\xfd\x01\x00\x00\x00\x00", 63);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 1);
    
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = p_st_sess;

    i_ret = __mysql_set_analysis_data(&st_mysql, &st_conn, &st_hash_report, &st_packet, p_new, 63, &st_time);
    
    frfree(p_data);
    frfree(p_new);
    mysql_destroy_session(&st_mysql.p_st_sess);
    __mysql_destroy_curr_info(&st_mysql);
    
    frhash2_free(&st_hash_report, frdb_statement_node_free);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_SUCC);
    frhash2_free(&st_hash_report, NULL);
    return;
}

int add_suite__mysql_set_analysis_data(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __mysql_set_analysis_data()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
